package com.giCompiler.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.giCompiler.bean.CodigoFonte;
import com.giCompiler.util.HibernateUtil;

public class CodigoFonteDao {

	public void inserir(CodigoFonte codigoFonte){ 
	       
		Session session = HibernateUtil.getSessionFactory().openSession(); 		       
		Transaction transaction = null; 		       
		       
		try { 
		            
			transaction = session.beginTransaction(); 		           
		    session.save(codigoFonte); 		           
			transaction.commit(); 
		        
		} catch (HibernateException e) { 
		           
			transaction.rollback(); 			           
			e.printStackTrace(); 
		      
		} finally { 		            
			session.close(); 	        
		} 

	}
	
	public void atualizar(CodigoFonte codigoFonte){ 
	       
		Session session = HibernateUtil.getSessionFactory().openSession(); 		       
		Transaction transaction = null; 		       
		       
		try { 
		            
			transaction = session.beginTransaction(); 		           
		    session.update(codigoFonte); 		           
			transaction.commit(); 
		        
		} catch (HibernateException e) { 
		           
			transaction.rollback(); 			           
			e.printStackTrace(); 
		      
		} finally { 		            
			session.close(); 	        
		} 

	}
	
	public void excluir(CodigoFonte codigoFonte){ 
	       
		Session session = HibernateUtil.getSessionFactory().openSession(); 		       
		Transaction transaction = null; 		       
		       
		try { 
		            
			transaction = session.beginTransaction(); 		           
		    session.delete(codigoFonte); 		           
			transaction.commit(); 
		        
		} catch (HibernateException e) { 
		           
			transaction.rollback(); 			           
			e.printStackTrace(); 
		      
		} finally { 		            
			session.close(); 	        
		} 

	}
	
	@SuppressWarnings("unchecked")
	public CodigoFonte pegarCodigoFonte(int idCodigoFonte){
			
    	Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<CodigoFonte> cf = new ArrayList <CodigoFonte> ();
		
		try {
			transaction = session.beginTransaction(); 
			cf = session.createCriteria(CodigoFonte.class).add(Restrictions.eq("idCodigoFonte", idCodigoFonte)).list();
	    									    
			transaction.commit(); 
    
		}catch (HibernateException e){
			
			transaction.rollback(); 			           
			e.printStackTrace();
			
		}finally{
			
			session.close();
			
		}
		
		return cf.get(0);
		
	}
	
	@SuppressWarnings("unchecked")
	public List<CodigoFonte> verificarSeExisteCF(int idCodigoFonte){
			
    	Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<CodigoFonte> cf = new ArrayList <CodigoFonte> ();
		
		try {
			transaction = session.beginTransaction(); 
			cf = session.createCriteria(CodigoFonte.class).add(Restrictions.eq("idCodigoFonte", idCodigoFonte)).list();
	    									    
			transaction.commit(); 
    
		}catch (HibernateException e){
			
			transaction.rollback(); 			           
			e.printStackTrace();
			
		}finally{			
			session.close();			
		}
		
		return cf;
		
	}
	
	@SuppressWarnings("unchecked")
	public List<CodigoFonte> pegarCodigoFontesDoProjeto(int idProjeto){
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<CodigoFonte> cfs = new ArrayList<CodigoFonte>();
		        
		try { 
		            
			transaction = session.beginTransaction(); 			           
							 			 
			Query query = session.createSQLQuery("SELECT cf.id_codigo_fonte, cf.codigo_fonte, cf.data_criacao, cf.data_modificacao, cf.id_pacote, cf.id_usuario, cf.nome FROM gicompiler.TB_PACOTE p, gicompiler.TB_CODIGO_FONTE cf WHERE p.ID_PROJETO = :idProjeto AND p.ID_PACOTE = cf.ID_PACOTE").addEntity(CodigoFonte.class);					 			 
			
			query.setInteger("idProjeto", idProjeto);
			cfs = query.list();

			transaction.commit(); 
		       
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
        return cfs;
	}
	
	@SuppressWarnings("unchecked")
	public List<CodigoFonte> pegarCodigoFontesDoPacote(int idPacote){
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<CodigoFonte> cfs = new ArrayList <CodigoFonte> ();
		        
		try { 

		    transaction = session.beginTransaction(); 
		    cfs = session.createCriteria(CodigoFonte.class).add(Restrictions.eq("idPacote", idPacote)).list();	    
			transaction.commit(); 
		
		
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
		
		return cfs;		
	}
	
	@SuppressWarnings("unchecked")
	public List<CodigoFonte> verificarDisponibilidadeNomeClasse(String nomeClasse, int idPacote, int idProjeto){
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<CodigoFonte> cfs = new ArrayList <CodigoFonte> ();
		        
		try { 
		            
			transaction = session.beginTransaction(); 			           
							 			 
			Query query = session.createSQLQuery("SELECT * FROM gicompiler.TB_PACOTE p, gicompiler.TB_CODIGO_FONTE cf WHERE p.ID_PROJETO = :idProjeto AND cf.ID_PACOTE = :idPacote AND UPPER(cf.nome) like UPPER(:nomeClasse)").addEntity(CodigoFonte.class);						 			 
			query.setInteger("idPacote", idPacote);
			query.setInteger("idProjeto", idProjeto);
			query.setString("nomeClasse", nomeClasse);
			cfs = query.list();

			transaction.commit(); 
		       
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
		
		return cfs;
		
	}
	
}
